package com.varian.service.config;

import com.varian.service.aspect.ResubmitAspect;
import com.varian.service.exception.BizExceptionResolve;
import com.varian.service.exception.VarianErrorAttributes;
import com.varian.service.remote.SysBizExceptionRemote;
import com.varian.tool.result.R;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;

/**
 * @author ben
 * @since 2024/6/23
 */
@AutoConfiguration
public class ServiceAutoConfig {

    @Bean
    @Primary
    public ErrorAttributes varianErrorAttributes() {
        return new VarianErrorAttributes();
    }

    @Bean
    public ResubmitAspect resubmitAspect() {
        return new ResubmitAspect();
    }

    @Bean
    @ConditionalOnMissingBean(BizExceptionResolve.class)
    public BizExceptionResolve defaultBizExceptionResolve(SysBizExceptionRemote remote) {
        return bizCode -> {
            R<String> result = remote.getMessageByCode(bizCode);
            return result.isSuccess() ? result.getData() : null;
        };
    }
}
